<template>
  <div>
    <!-- <div class="list-table" style="margin-bottom:20px;">
      <el-switch
        v-model="cashflowQuarterOrMonth"
        :active-text="$t('按月统计')"
        :inactive-text="$t('按季统计')"
      />
      <cashflow ref="cashflow" :quarter-or-month="cashflowQuarterOrMonth" />
      <el-row class="cashflow">
        <el-col :span="8">
          <div class="">IRR
            <el-tooltip placement="bottom">
              <div slot="content">
                {{ $t('根据现金流自动计算') }}<br>
              </div>
              <el-button class="tipText" type="text">
                <i class="el-icon-question" style="color: #595a5a;" />
              </el-button>
            </el-tooltip>
          </div>
          <div v-if="cashflowVO" class="">{{ (cashflowVO.irr !== 0 && cashflowVO.irr !== null ? cashflowVO.irr: '-') }}</div>
        </el-col>
        <el-col :span="8">
          <div class="">DPI
            <el-tooltip placement="bottom">
              <div slot="content">
                {{ $t('出资人分配金额 / 出资人实缴金额') }}
              </div>
              <el-button class="tipText" type="text">
                <i class="el-icon-question" style="color: #595a5a;" />
              </el-button>
            </el-tooltip>
          </div>
          <div v-if="cashflowVO" class="">{{ (cashflowVO.dpi !== 0 && cashflowVO.dpi !== null ? cashflowVO.dpi: '-') }}</div>
        </el-col>
        <el-col :span="8">
          <div class="">MOIC
            <el-tooltip placement="bottom">
              <div slot="content">
                {{ $t('流入现金流 / 流出现金流') }}
              </div>
              <el-button class="tipText" type="text">
                <i class="el-icon-question" style="color: #595a5a;" />
              </el-button>
            </el-tooltip>
          </div>
          <div v-if="cashflowVO" class="">{{ (cashflowVO.moic !== 0 && cashflowVO.moic !== null ? cashflowVO.moic: '-') }}</div>
        </el-col>
      </el-row>
    </div> -->
    <div class="list-table">
      <div class="area-header">
        <span class="area-header-title">{{ $t('出资人现金流') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('出资人名称')" prop="lpName">
          <el-input v-model="queryParams.lpName" :placeholder="$t('出资人名称')" />
        </el-form-item>
        <el-form-item :label="$t('现金流类型')" prop="type">
          <el-select v-model="queryParams.type" :placeholder="$t('现金流类型')" clearable>
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('发生时间')" prop="operateDate">
          <DatePicker
            v-model="dateRange"
            type="daterange"
          />

        </el-form-item>
        <el-form-item :label="$t('现金流方向')" prop="direction">
          <el-select v-model="queryParams.direction" :placeholder="$t('现金流方向')" clearable>
            <el-option
              v-for="dict in directionOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('业务类型')" prop="businessType">
          <el-select v-model="queryParams.businessType" :placeholder="$t('业务类型')" clearable>
            <el-option
              v-for="dict in businessTypeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('IRR计算')" prop="whetherIrr">
          <el-select v-model="queryParams.whetherIrr" :placeholder="$t('IRR计算')" clearable>
            <el-option
              v-for="dict in whetherIrrOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['lpbase:cashflow:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['lpbase:cashflow:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            v-hasPermi="['lpbase:cashflow:view']"-->
        <!--            type="primary"-->
        <!--            icon="el-icon-info"-->
        <!--            size="mini"-->
        <!--            @click="getIrr"-->
        <!--          >{{ $t("获取IRR") }}</el-button>-->
        <!--        </el-col>-->
      </el-row>

      <el-table v-loading="loading" :data="lpCashflowList" border>
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资人名称')" align="center" prop="lpName" show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.lpName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('现金流类型')" align="center" prop="type" :formatter="typeFormat" show-overflow-tooltip width="120" />
        <el-table-column :label="$t('发生时间')" align="center" prop="operateDate" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.operateDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('发生金额') + $t('(元)')" align="center" prop="operateAmount" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat((scope.row.direction=== 'LC'? 0- scope.row.operateAmount: scope.row.operateAmount), undefined, 2, true) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="isRate" :label="$t('汇率')" align="center" prop="exchangeRate" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.exchangeRate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="isRate" :label="$t('发生金额') + $t('RMB') + $t('(元)')" align="center" prop="operateRmb" show-overflow-tooltip width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.operateRmb, undefined, 2, true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('业务类型')" align="center" prop="businessType" :formatter="businessTypeFormat" show-overflow-tooltip width="120" />
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
          <template slot="header">
            <span>{{ $t('操作') }}</span>
            <!--            <el-tooltip placement="top">-->
            <!--              <div slot="content">-->
            <!--                {{ $t('不是从现金流功能添加的数据，不能修改与删除，如需修改请修改来源数据') }}-->
            <!--              </div>-->
            <!--              <i class="el-icon-question" />-->
            <!--            </el-tooltip>-->
          </template>
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['lpbase:cashflow:edit']"
              size="small"
              type="text"
              :icon=" 'el-icon-edit'"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改出资人现金流对话框 -->
      <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
        <LpCashflowForm ref="LpCashflowForm" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listLpCashflow, delLpCashflow, getLpCashflowIrr } from '@/api/fund/lpBase/cashflow/lpCashflow'
import LpCashflowForm from '../cashflow/lpCashflowForm'
import { getFundBase } from '@/api/fund/fundBase'
import { getSystemCurrency } from '@/api/system/config'
// import cashflow from '@/views/chart/lpCashflow'
import { getCashFlowCombined } from '@/api/fund/lpBase/cashflow/lpCashflow'
export default {
  components: {
    LpCashflowForm
    // cashflow
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 出资人现金流表格数据
      lpCashflowList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      isRate: false,
      fundCurr: '',
      defCurr: '',
      // 发生时间范围
      dateRange: [],
      // 现金流类型  预计 实际字典
      typeOptions: [],
      // 方向：流入、流出字典
      directionOptions: [],
      // 业务类型：基金缴款 基金分配字典
      businessTypeOptions: [],
      // 是否参与IRR计算字典
      whetherIrrOptions: [],
      // 查询参数
      queryParams: {
          type: undefined,
          lpName: undefined,
          fundId: this.$route.query.id,
          operateDate: undefined,
          direction: undefined,
          businessType: undefined,
          whetherIrr: 'Y',
          pageNum: 1,
          pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      cashflowVO: {},
      cashflowQuarterOrMonth: false
    }
  },
  created() {
      this.initCurr()
    // 查询基金现金流合计信息
    this.getCashFlowCombined()
      this.getList()
    // 现金流类型
      this.getDicts('cash_flow_type').then(response => {
          this.typeOptions = response.data
      })
      this.getDicts('cash_flow_direction').then(response => {
          this.directionOptions = response.data
      })
      this.getDicts('lpbase_cashflow_business_type').then(response => {
          this.businessTypeOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
          this.whetherIrrOptions = response.data
      })
  },
  methods: {
    /** 查询出资人现金流列表 */
    getList() {
        this.loading = true
        listLpCashflow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.lpCashflowList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 现金流类型  预计 实际字典翻译
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 方向：流入、流出字典翻译
    directionFormat(row) {
      return this.selectDictLabel(this.directionOptions, row.direction)
    },
    // 业务类型：基金缴款 基金分配字典翻译
    businessTypeFormat(row) {
      return this.selectDictLabel(this.businessTypeOptions, row.businessType)
    },
    // 是否参与IRR计算字典翻译
    whetherIrrFormat(row) {
      return this.selectDictLabel(this.whetherIrrOptions, row.whetherIrr)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            lpId: undefined,
            fundId: undefined,
            type: undefined,
            operateDate: undefined,
            operateAmount: undefined,
            direction: '0',
            businessType: undefined,
            whetherIrr: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
            this.$refs.LpCashflowForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.LpCashflowForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delLpCashflow(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/cashflow/lpCashflow/export', this.addDateRange(this.queryParams, this.operateDates))
    },
    getIrr() {
      getLpCashflowIrr(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        let msg = ''
        if (res.data === 502) {
          msg = this.$t('计算失败：小于或等于一条现金流')
        } else if (res.data === 503) {
          msg = this.$t('计算失败：第一条现金流的时间不是最早时间')
        } else if (res.data === 504) {
          msg = this.$t('计算失败：第一条现金流的payment的值不为负')
        } else {
          msg = res.data
        }
        this.$alert(msg, '当前数据IRR', {
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 初始化币种信息
    async initCurr(val) {
      let param = this.$route.query.id
      if (this.$route.query.id === undefined) {
        param = val
      }
      await getFundBase(param).then(res => {
        this.fundCurr = res.data.currency
      })
      await getSystemCurrency().then(res => {
        if (res.code === 200) {
          this.defCurr = res.data
        } else {
          this.msgError(res.msg)
        }
      })
      this.isRate = this.defCurr !== this.fundCurr
    },
    // 查询irr、dpi和moic
    getCashFlowCombined() {
      if (this.queryParams.fundId !== undefined) {
        getCashFlowCombined(this.queryParams.fundId).then(response => {
          this.cashflowVO = response.data
        })
      }
    }
  }
}
</script>
